
import java.util.concurrent.atomic.AtomicReference;

public class SpinLock {

    AtomicReference<Thread> automaticReference = new AtomicReference<>();

    public void myLock () {
        Thread thread = Thread.currentThread();
        while (! automaticReference.compareAndSet(null, thread)) {}
    }

    public void myUnLock () {
        Thread thread = Thread.currentThread();
        automaticReference.compareAndSet(thread, null);
    }
}
